<?php
namespace Admin\Controller;
use Think\Controller;
class MemberController extends Controller {
    protected $model;
    public function _initialize(){
        $this->model = D('Member');
    }
    //查询数据，进入列表页
    public function index(){
        //获取会议列表
        $aid = session('admin.id');
        $clist = M('Conference')->where('aid='.$aid)->order('id desc')->select();
        $this->assign('clist',$clist);
        //获取参会人员列表
        $data['m.aid'] = session('admin.id');
        $get = I('get.');
        //var_dump($get);exit;
        if(!empty($get['cid'])){
            $data['cid'] = array('eq',$get['cid']);
        }
        if(!empty($get['company'])){
            $data['company'] = array('like','%'.$get['company'].'%');
        }
        if(!empty($get['position'])){
            $data['position'] = array('like','%'.$get['position'].'%');
        }
        $this->assign('where',$get);
        $count  = $this->model->alias('m')->where($data)->count();
        $Page   = new \Think\Page($count,10);
        $show   = $Page->show();
        $list = $this->model->alias('m')->join('left join conference c on m.cid=c.id')->field('m.*,c.name as cname')->where($data)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('Data',json_encode($list));
        $this->display("Member/index");
        if(!empty($get['excel'])){
            $list = $this->model->alias('m')->join('left join conference c on m.cid=c.id')->field('m.*,c.name as cname')->where($data)->order('id desc')->select();

            $objPHPExcel = new \PHPExcel\Spreadsheet();
            $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
            $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
            $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1','name')
                ->setCellValue('B1','tel')
                ->setCellValue('C1','email')
                ->setCellValue('D1','company')
                ->setCellValue('E1','position')
                ->setCellValue('F1','is_kind')
                ->setCellValue('G1','is_arrive');
            foreach ($list as $k=>$v){
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A'.($k+2) , $v['name'])
                    ->setCellValue('B'.($k+2) , $v['tel'])
                    ->setCellValue('C'.($k+2) , $v['email'])
                    ->setCellValue('D'.($k+2) , $v['company'])
                    ->setCellValue('E'.($k+2) , $v['position'])
                    ->setCellValue('F'.($k+2) , $v['is_kind'])
                    ->setCellValue('G'.($k+2) , $v['is_arrive']);
            }
            $objPHPExcel->getActiveSheet()->setTitle('Simple');//设置文件的标题
            $objPHPExcel->getActiveSheetIndex(0);//设置当前工作表
            header("Content-Type:application/vnd.ms-execl");
            header('Content-Disposition:attachment;filename="resume.xls"');
            header("Cache-Control:max-age=0");
            header("Cache-Control:max-age=1");
            header("Expires:Mon,26 Jul 1997 05:00:00 GMT");//data in the past
            header("Last-Modified:".gmdate('D, d M Y H:i:s').' GMT');//always modified
            header("Cache-Control: cache,must-revalidate");//HTTP/1.1
            header("Pragma: public");//HTTP/1.0
            $objWriter = \PHPExcel\IOFactory::createWriter($objPHPExcel,'Excel5');
            $objWriter->save('php://output');
        }

    }
    //进入添加页
    public function add(){
        $aid = session('admin.id');
        $clist = M('Conference')->where('aid='.$aid)->order('id desc')->select();
        $this->assign('clist',$clist);
        $this->display("Member/add");
    }
    //进入批量加载页
    public function addlist(){
        $aid = session('admin.id');
        $clist = M('Conference')->where('aid='.$aid)->order('id desc')->select();
        $this->assign('clist',$clist);
        $this->display("Member/addlist");
    }
    //根据id查询数据，进入修改页
    public function edit(){
        $aid = session('admin.id');
        $clist = M('Conference')->where('aid='.$aid)->order('id desc')->select();
        $this->assign('clist',$clist);
        $id = I('get.id/d',0);
        $data = $this->model->where('id='.$id)->find();
        $this->assign('data',$data);
        $this->display("Member/add");
    }
    //执行添加或修改操作
    public function post(){
        $data = I('post.');
        if(empty($data['id'])){
            if($this->model->create($data)){
                if($this->model->add()!==false){
                    retJson("200","添加成功！");
                }else{
                    retJson("404","添加失败！");
                }
            }else{
                retJson("404",$this->model->getError());
            }
        }else{
            if($this->model->create($data)){
                if($this->model->save()!==false){
                    retJson("200","修改成功！");
                }else{
                    retJson("404","修改失败！");
                }
            }else{
                retJson("404",$this->model->getError());
            }

        }
    }
    //执行删除
    public function del(){
        $id = I('get.id',0);
        if($this->model->where('id='.$id)->delete()){
            retjson("200","删除成功！");
        }else{
            retjson("404","删除失败！");
        }
    }
    //执行批量加载
    public function postexcel(){
        /*var_dump(I('post.'));
        var_dump($_FILES);
        exit;*/
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('xls', 'xlsx');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
/*array(1) {
    ["logo1"]=>array(9) {
        ["name"]=> "会议签到-数据表设计.xlsx"
        ["type"]=> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        ["size"]=>int(13124)
        ["key"]=>"logo1"
        ["ext"]=> "xlsx"
        ["md5"]=> "ac7934bf76cac816f2dea7314b41cdf3"
        ["sha1"]=> "d8f28ba251cef852ae80ff68b3c51363aa1f0e25"
        ["savename"]=> "578b71ce73e6e.xlsx"
        ["savepath"]=> "2016-07-17/"
  }
}*/
        if(!$info) {// 上传错误提示错误信息
            retJson("404",$upload->getError());
        }else{// 上传成功
            //$this->success('上传成功！');
            $filename = './Uploads/'.$info['excel']['savepath'].$info['excel']['savename'];
            $exts = $info['ext'];
            //var_dump($filename);exit;
            $this->daoru($filename);
        }
    }
    //处理excel文件，并执行数据添加
    public function daoru($url){
        $admin = I('session.admin');
        $add['aid'] = $admin['id'];
        $add['cid']=I('post.cid');
        $objPHPExcel = \PHPExcel\IOFactory::load($url);
        $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
        $count = count($sheetData);
        for($i=2;$i<=$count;$i++){
            $add['name']=$sheetData[$i]['A'];
            $add['tel']=$sheetData[$i]['B'];
            $add['email']=$sheetData[$i]['C'];
            $add['company']=$sheetData[$i]['D'];
            $add['position']=$sheetData[$i]['E'];
            $add['is_kind']=$sheetData[$i]['F'];
            $add['is_arrive']=$sheetData[$i]['G'];
            $this->model->add($add);
        }
        retjson("200","导入成功！");
    }
    //执行Excel导出
    public function download(){
        //获取参会人员列表
        $data['m.aid'] = session('admin.id');
        $get = I('$get.');
        if(!empty($get['cid'])){
            $data['cid'] = array('eq',$get['cid']);
        }
        if(!empty($get['company'])){
            $data['company'] = array('like','%'.$get['company'].'%');
        }
        if(!empty($get['position'])){
            $data['position'] = array('like','%'.$get['position'].'%');
        }
        $list = $this->model->alias('m')->join('left join conference c on m.cid=c.id')->field('m.*,c.name as cname')->where($data)->order('id desc')->select();


        $objPHPExcel = new \PHPExcel\Spreadsheet();
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1','name')
            ->setCellValue('B1','tel')
            ->setCellValue('C1','email')
            ->setCellValue('D1','company')
            ->setCellValue('E1','position')
            ->setCellValue('F1','is_kind')
            ->setCellValue('G1','is_arrive');

        foreach ($list as $k=>$v){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.($k+2) , $v['name'])
                ->setCellValue('B'.($k+2) , $v['tel'])
                ->setCellValue('C'.($k+2) , $v['email'])
                ->setCellValue('D'.($k+2) , $v['company'])
                ->setCellValue('E'.($k+2) , $v['position'])
                ->setCellValue('F'.($k+2) , $v['is_kind'])
                ->setCellValue('G'.($k+2) , $v['is_arrive']);
        }

        $objPHPExcel->getActiveSheet()->setTitle('Simple');//设置文件的标题
        $objPHPExcel->getActiveSheetIndex(0);//设置当前工作表
        header("Content-Type:application/vnd.ms-execl");
        header('Content-Disposition:attachment;filename="resume.xls"');
        header("Cache-Control:max-age=0");
        header("Cache-Control:max-age=1");
        header("Expires:Mon,26 Jul 1997 05:00:00 GMT");//data in the past
        header("Last-Modified:".gmdate('D, d M Y H:i:s').' GMT');//always modified
        header("Cache-Control: cache,must-revalidate");//HTTP/1.1
        header("Pragma: public");//HTTP/1.0
        $objWriter = \PHPExcel\IOFactory::createWriter($objPHPExcel,'Excel5');
        $objWriter->save('php://output');
    }

}